package com.xingchen.loveparttimejob.controller.servlet.merchant;

import com.xingchen.loveparttimejob.controller.MyCustomRuntimeException;
import com.xingchen.loveparttimejob.controller.servlet.BaseServlet;
import com.xingchen.loveparttimejob.model.pojo.JobInfo;
import com.xingchen.loveparttimejob.model.pojo.JobRelease;
import com.xingchen.loveparttimejob.model.vo.JobReleaseVo;
import com.xingchen.loveparttimejob.model.vo.ResponseData;
import com.xingchen.loveparttimejob.service.JobInfoService;
import com.xingchen.loveparttimejob.service.JobReleaseService;
import com.xingchen.loveparttimejob.service.impl.JobInfoServiceImpl;
import com.xingchen.loveparttimejob.service.impl.JobReleaseServiceImpl;
import com.xingchen.loveparttimejob.uitls.*;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 获取职用户发布的职位信息接口
 * @author 小志
 * @date 2024/12/9 - 10:40
 */

@WebServlet("/api/merchant/getJobInfoList")
public class GetMerchantJobInfoList extends BaseServlet {
    static final JobReleaseService JOB_RELEASE_SERVICE = new JobReleaseServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //在这里把获取的信息拼成用户界面显示的数据
        /*
        * 在此之前需要先验证用户的令牌
        * 1、声明一个JobInfoVo的list集合用来存储获取到的数据
        * 2、获取里面的信息，第一步必须获取基本信息，拿到我们要显示的几条信息
        * 3、然后拿到它的职位id然后去到每一个service中获取每一条信息
        * 4、声明一个分页工具类，类型为JobInfoVo,用来存储类型为显示给商家发布兼职信息的类
        * 5、把list集合赋值给工具类，并转换成json格式，发送给前端
        * */
        JobInfoService jobInfoService = new JobInfoServiceImpl();
//        String token = request.getHeader("Authorization");
        PagerUtil<JobReleaseVo> jobReleaseVo = new PagerUtil<>();
        ResponseData responseData;
        try {
            String indexPage = request.getParameter("indexPage");
            String showPage = request.getParameter("showRows");
            String search = request.getParameter("search");


            jobReleaseVo.setIndexPage(Integer.parseInt(indexPage));
            jobReleaseVo.setShowRows(Integer.parseInt(showPage));
            jobReleaseVo.setSearch(search);

            PagerUtil<JobRelease> jobRelease = new PagerUtil<>();
            jobRelease.setIndexPage(Integer.parseInt(indexPage));
            jobRelease.setShowRows(Integer.parseInt(showPage));
            jobRelease.setSearch(search);


//            String userId = JWTUtils.generateJwtUtilsObject(token).getUserId();
            String userId = "4";
            List<JobRelease> jobReleaseList = JOB_RELEASE_SERVICE.getSelectJobReleaseList(jobRelease, userId, false);
            ArrayList<JobReleaseVo> jobReleaseVos = new ArrayList<>(); //5
            int i = 1;
            boolean closeDB = false;
            for (JobRelease release : jobReleaseList) {
                JobReleaseVo jrv = new JobReleaseVo();
                // 1、获取用户发布的所有兼职信息
                JobInfo jobInfoData = jobInfoService.getJobInfoData(release.getJiId(), closeDB);
                jrv.setJobInfo(jobInfoData);
                // 2、获取兼职类型

                // 3、获取工作条件

                // 4、获取工作时间

                if(jobReleaseVos.size() == i ){
                    closeDB = true;
                }
                i++;
                // 5、获取工作福利

                jobReleaseVos.add(jrv);
            }

            jobReleaseVo.setData(jobReleaseVos);
            responseData = successJson(200, "success","获取成功", jobReleaseVo);

        }catch (MyCustomRuntimeException e){
            //返回错误信息给前端
            responseData = errorJson(500, "error", e.getMessage(), "");
        }

        print(response,responseData);
    }
}
